package org.mc.domainservice;

import org.mc.bean.Account;
import org.mc.repository.AccountRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;

/**
 * Created by luoxf on 2018/9/13.
 */
@Service
public class AccountDomainService {

    @Autowired
    private AccountRepository accountRepository;

    @Transactional
    public void addAccount() {
        Account account = accountRepository.findOne(1L);
        account.setMoney(account.getMoney().add(new BigDecimal(100)));
        accountRepository.save(account);
    }

    @Transactional
    public void subAccount() {
        Account account = accountRepository.findOne(1L);
        account.setMoney(account.getMoney().subtract(new BigDecimal(100)));
        accountRepository.save(account);
    }


    //private Executor executor = Executors.newFixedThreadPool(20);  //创建固定容量大小的缓冲池
    private Executor executor = Executors.newCachedThreadPool();        //创建一个缓冲池，缓冲池容量大小为Integer.MAX_VALUE
    //private Executor executor = Executors.newSingleThreadExecutor();   //创建容量为1的缓冲池

    int count = 1;

    @Async
    public void aa() {
        int count = 1;
        /*executor.execute(new Runnable() {
            @Override
            public void run() {
                try {
                    Thread.sleep(5000L);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("线程：" + Thread.currentThread() + "负责了" + count + "次任务");
            }
        });*/
        try {
            Thread.sleep(5000L);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("线程：" + Thread.currentThread() + "负责了" + count + "次任务");
        count++;

    }

    @Async
    public void bb() {
        System.out.println("bb");
    }

}
